// Created on 16 juil. 07
package com.cofluent.web.server;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URISyntaxException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Christophe MOINE
 */
public class RegisterVersion extends HttpServlet
{
	@Override
	public void init() throws ServletException
	{
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException
	{
//		String releaseNumber=req.getParameter("release-number");
//		String buildNumber=req.getParameter("build-number");
//		String cvsTag=req.getParameter("cvs-tag");
//		String versionNumber=req.getParameter("version-number");
		PrintWriter out = resp.getWriter();
//		out.println(releaseNumber);
//		out.println(buildNumber);
//		out.println(cvsTag);
//		out.println(versionNumber);
		if(BuildInformationServiceImpl.INPUT_FILE_URL==null)
		{
			out.println("Failed to open input stream");
		}
		else
		{
			try
			{
				InputStream inputStream=BuildInformationServiceImpl.INPUT_FILE_URL.openStream();
				BuildElements buildElements=(BuildElements)XMLSerializerUtil.unmarshal(BuildElements.class, inputStream);
				inputStream.close();
				BuildElement buildElement=buildElements.createAndAddBuildElement();
				buildElement.setValues(req.getParameterMap());
//				buildElement.setReleaseNumber(releaseNumber);
//				buildElement.setBuildNumber(buildNumber);
//				buildElement.setVersionNumber(versionNumber);
				// write file:
				File file=new File(BuildInformationServiceImpl.INPUT_FILE_URL.toURI());
//				OutputStream outputStream=BuildInformationServiceImpl.INPUT_FILE_URL.openConnection().getOutputStream();
				XMLSerializerUtil.marshall(buildElements, file);
//				outputStream.close();
				out.println("ok");
			}
			catch (IOException e)
			{
				out.println(e.getMessage());
				e.printStackTrace();
			}
			catch (URISyntaxException e)
			{
				out.println(e.getMessage());
				e.printStackTrace();
			}
		}
//        out.println("<html><head><title>Version Registring</title></head><body>");
//        out.println("");
//        out.println("</body></html>");
        out.close();
	}
	
	@Override
	public void destroy()
	{
	}
}
